Next.js template
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

[...nextauth].js 1016B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import NextAuth from 'next-auth';
  2. import Credentials from 'next-auth/providers/credentials';
  3. import { connectToDatabase } from '../../../utils/helpers/dbHelpers';
  4. import { verifyPassword } from '../../../utils/helpers/hashPasswordHelpers';
  5. export default NextAuth({
  6. session: {
  7. jwt: true,
  8. },
  9. providers: [
  10. Credentials({
  11. async authorize(credentials) {
  12. const client = await connectToDatabase();
  13. const usersCollection = client.db().collection('users');
  14. const user = await usersCollection.findOne({
  15. username: credentials.username,
  16. });
  17. if (!user) {
  18. client.close();
  19. throw new Error('No user found!');
  20. }
  21. const isValid = await verifyPassword(
  22. credentials.password,
  23. user.password
  24. );
  25. if (!isValid) {
  26. client.close();
  27. throw new Error('Could not log you in!');
  28. }
  29. client.close();
  30. return { name: user.fullName };
  31. },
  32. }),
  33. ],
  34. });